<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./lib/bootstrap.css" />
  <script src="./lib/jquery.js"></script>
</head>

<body>
  <!-- 添加图书的Panel面板 -->
  <div class="panel panel-primary">
    <div class="panel-heading">
      <h3 class="panel-title">添加新图书</h3>
    </div>
    <div class="panel-body form-inline">

      <div class="input-group">
        <div class="input-group-addon">书名</div>
        <input type="text" class="form-control" id="iptBookname" placeholder="请输入书名">
      </div>

      <div class="input-group">
        <div class="input-group-addon">作者</div>
        <input type="text" class="form-control" id="iptAuthor" placeholder="请输入作者">
      </div>

      <div class="input-group">
        <div class="input-group-addon">出版社</div>
        <input type="text" class="form-control" id="iptPublisher" placeholder="请输入出版社">
      </div>

      <button id="btnAdd" class="btn btn-primary">添加</button>

    </div>
  </div>

  <!-- 图书的表格 -->
  <table class="table table-bordered table-hover">
    <thead>
      <tr>
        <th>Id</th>
        <th>书名</th>
        <th>作者</th>
        <th>出版社</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody id="tb">
      <!-- <tr>
        <td>704</td>
        <td>日落大道</td>
        <td>卡比丘</td>
        <td>长江出版社</td>
        <td><a href="javascript:;" class="del" data-id="704">删除</a></td>
      </tr> -->
    </tbody>
  </table>

  <script>
    $(function () {
      // 获取图书列表数据
      function fn() {
        $.ajax({
          type: 'GET',
          url: 'http://www.liulongbin.top:3006/api/getbooks',
          success({ status, msg, data }) {
            if (status !== 200) {
              return alert('获取失败')
            }
            const htmlStr = data.map(function (item) {
              const { id, bookname, author, publisher } = item
              return `
              <tr>
        <td>${id}</td>
        <td>${bookname}</td>
        <td>${author}</td>
        <td>${publisher}</td>
        <td><a href="javascript:;" class="del" data-id="${id}">删除</a></td>
      </tr>
              `
            }).join('')
            $('#tb').html(htmlStr)
          }
        })
      }
      fn()

      // 删除功能
      $('#tb').on('click', '.del', function () {
        const id = $(this).attr('data-id')
        $.ajax({
          type: 'DELETE',
          url: `http://www.liulongbin.top:3006/api/delbook?id=${id}`,
          success({ status, msg }) {
            if (status !== 200) {
              return alert(msg)
            }
            fn()
          }
        })
      })
    })

    // 添加功能
    $('#btnAdd').click(function () {
      const bookname = $('#iptBookname').val().trim()
      const author = $('#iptAuthor').val().trim()
      const publisher = $('#iptPublisher').val().trim()
      $.ajax({
        type: 'POST',
          url: 'http://www.liulongbin.top:3006/api/addbook',
          data: {
            bookname, author, publisher
          },
          success({status, msg}) {
            if (status !== 201) {
              return alert(msg)
            }
            fn()
          }
      })
    })
  </script>
</body>

</html>